<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit notas.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Notas_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["nota"] = new Nota($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["nota"]->id);

    // To one relationships
        
    $usuarios = new Usuario();
    $data["usuarios"] = $usuarios->all_to_asoc_array("id","email");
    $data["usuarios_selected"] = $data["nota"]->usuario->id;
    
    // To many relationships
    
    $this->smarty_parser->view('nota_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["nota"] = new Nota($id);
    
    $data["nota"]->titulo = $this->input->post("titulo");
    
    $data["nota"]->descripcion = $this->input->post("descripcion");
    
    $data["nota"]->modificado = $this->input->post("modificado");
    
    //saving attributes
    if(! $data["nota"]->save())
      $data["error"] .= $data["nota"]->error->string;

    //saving to one relationships
    
    $usuario = new Usuario($this->input->post("usuario"));
    if (! $data["nota"]->save(array($usuario)))
      $data["error"] .= $data["nota"]->error->string;
    
    //to many relationships
    
    redirect(base_url()."notas/edit/".$data["nota"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["nota"] = new Nota($id);

    //delete
    if(! $data["nota"]->delete())
      $data["error"] .= $data["nota"]->error->string;

    redirect(base_url()."notas/edit/");
  } // end of method delete($id)

    
} // class Notas_protoweb

/* End of file notas_protoweb.php */
/* Location: ./system/application/controllers/notas_protoweb.php */
